package com.example.train.service.impl;

import com.example.train.service.SysAdminRoleRelationService;
import com.example.train.service.SysRolePermissionRelationService;
import com.example.train.service.SysRoleService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.example.train.model.SysRole;
import com.example.train.mapper.SysRoleMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Yolo
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysAdminRoleRelationService sysAdminRoleRelationService;

    @Resource
    private SysRolePermissionRelationService sysRolePermissionRelationService;

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int deleteByPrimaryKey(Long id) {
        sysAdminRoleRelationService.deleteByRoleId(id);
        sysRolePermissionRelationService.deleteByRoleId(id);
        return sysRoleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer insert(SysRole record) {
        SysRole temp = sysRoleMapper.selectByName(record.getName());
        if (temp != null) {
            return null;
        }
        record.setSort(0);
        record.setCreateTime(new Date());
        return sysRoleMapper.insert(record);
    }

    @Override
    public int insertSelective(SysRole record) {
        return sysRoleMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(SysRole record) {
        return sysRoleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SysRole record) {
        return sysRoleMapper.updateByPrimaryKey(record);
    }

    @Override
    public SysRole selectByPrimaryKey(Long id) {
        return sysRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysRole selectByName(String name) {
        return sysRoleMapper.selectByName(name);
    }

    @Override
    public List<SysRole> selectByAdminID(Long id) {
        return sysRoleMapper.selectByAdminID(id);
    }

    @Override
    public List<SysRole> selectAll(Integer pageNum, Integer pageSize) {
        if (null != pageNum && null != pageSize) {
            PageHelper.startPage(pageNum, pageSize);
        }
        return sysRoleMapper.selectAll();
    }
}
